//
//  WelcomeController.swift
//  Swiift_SinaWeibo
//
//  Created by bear on 16/5/22.
//  Copyright © 2016年 bear. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setUI()
        
        
        view.backgroundColor=UIColor.whiteColor()
        

        
        
        
        
        
    }
    
    
    
    func setUI() {
        
        iconView.layer.cornerRadius=45;
        iconView.clipsToBounds=true
    
        self.view.addSubview(iconView)
        self.view.addSubview(nameLabel)
        nameLabel.alpha=0;
        
        
        iconView.snp_makeConstraints { (make) in
            
            
            make.size.equalTo(CGSizeMake(90, 90))
            
            make.bottom.equalTo(SCREEN_WIDTH-200)
            make.centerX.equalTo(view.snp_centerX)
            
            
        }
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconView.snp_centerX)
            
            make.centerY.equalTo(iconView.snp_centerY).offset(70);
        }
        
        
        
        UIView.animateWithDuration(0.5) { 
            
            self.iconView.snp_makeConstraints(closure: { (make) in
                
               
                
                
                
            })
            
        }
        
        
        
    }
    
    
    
    
    
    //界面已经出现的时候会自动调用
    //这个方法是页面显示之后的生命周期方法 在这个方法中所有的子视图的位置 已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    private func startAnimation() {
        
        let offsetY = -UIScreen.mainScreen().bounds.height + 180
        
        self.iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offsetY)
            
        }
        

        
        
        
        //阻尼  弹簧系数  值越小效果越大     //加速度
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            
            self.view.layoutIfNeeded()
            self.nameLabel.alpha=1;
        }) { (_) -> Void in
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                self.nameLabel.alpha = 1
                }, completion: { (_) -> Void in
                    
                    
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(SWITCH_ROOT_VIEWCONTROLLER_NOTIFICATION, object: nil)
                    
                    
            })
        }
    }
    
    
    

    
    private lazy var iconView:UIImageView = {
        
        let iconUrlString =  (UserAccountViewModel.shareUserAccountViewModel.userAccount?.avatar_large) ?? ""
        
        
        let imgV = UIImageView(image: UIImage(named: "avatar_default_big"))
        
           imgV.sd_setImageWithURL(NSURL(string: iconUrlString)!)
        
        print("-------       "+iconUrlString+"    ------>>>>>>"+(UserAccountViewModel.shareUserAccountViewModel.userAccount?.name ??   "没有读取到") )
        
        return imgV
    }()
    
    private lazy var nameLabel:UILabel = UILabel(text: UserAccountViewModel().userAccount?.name ?? "", textColor: UIColor.darkGrayColor(), fontSize: 24)

}
